package com.attendancesystem.controller;

import com.attendancesystem.model.Student;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.*;



import javax.servlet.annotation.WebServlet;
import java.io.IOException;


@WebServlet(name = "/LoginServlet", value = "/LoginServlet")
public class LoginController extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        Student student = new Student();
        String role = request.getParameter("role");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
//        System.out.println("role:"+role);

        // 根据角色跳转
        switch (role) {
            case "Student":
                // 添加示例验证逻辑

//                System.out.println("username:"+username);
                HttpSession StudentSession = request.getSession();
                StudentSession.setAttribute("username", username); // 存储学号到session
                response.sendRedirect("StudentInfoController"); // 跳转
//                request.getRequestDispatcher("/WEB-INF/views/Student.jsp").forward(request, response);



//                }

                break;
            case "Teacher":

                // 使用请求转发（forward）
                HttpSession TeacherSession = request.getSession();
                TeacherSession.setAttribute("username", username);
                response.sendRedirect("TeacherInfoController");


//                request.getRequestDispatcher("/WEB-INF/views/Teacher.jsp").forward(request, response);

                break;
            case "Admin":
                // 使用请求转发（forward）
                HttpSession AdminSession = request.getSession();
                AdminSession.setAttribute("username", username);
                response.sendRedirect("AdminInfoController");


                break;
            default:
                // 在 default case 中添加错误处理
                request.setAttribute("error", "请选择有效用户角色");
                request.getRequestDispatcher("/login.jsp").forward(request, response);

                break;
        }
    }
}